package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: mini server
 * Date: 9/20/13
 * Time: 11:17 AM
 * To change this template use File | Settings | File Templates.
 */
public class Library
{
    public static final int NEW_INSTANCE = 0;          // NEED TO CONVERT THIS TO ENUM
    public static final int OLD_INSTANCE = 1;

    private static Library library;

    private ArrayList<Inventory> inventory;

    private Library()
    {
        inventory = new ArrayList<Inventory>();
        inventory.add(new BookInventory());
        inventory.add(new MovieInventory());
    }

    public static Library GetInstanceOfLibrary(int kindOfInstance)
    {
        if(library == null)
        {
            library = new Library();
            return library;
        }

        switch (kindOfInstance)
        {
            case NEW_INSTANCE: {
                library = new Library();
                return library;
            }
            case OLD_INSTANCE: return library;
        }

        return null;
    }

    public boolean reserve(int inventoryCode, String id)
    {
        Iterator<Inventory> iteratorOverInventories = inventory.iterator();

        while(iteratorOverInventories.hasNext())
        {
            Inventory inventory = iteratorOverInventories.next();
            if(inventory.typeOfInventory() == inventoryCode)
            {
                if(inventory.reserveAnItem(id))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public String list(int inventoryCode)      // Not thoroughly tested.
    {
        Iterator<Inventory> iteratorOverInventories = inventory.iterator();
        String result = "";

        while(iteratorOverInventories.hasNext())
        {
            Inventory inventory = iteratorOverInventories.next();
            if(inventory.typeOfInventory() == inventoryCode)
            {
                result = result + inventory.list();
            }
        }

        return result;
    }
}
